#!/usr/bin/env python
#coding:utf-8

import wx
import Globals
import ControlThings as CT
import BusyInfo as PBI
import images

class SensorCylinderListener(CT.CylinderListener):
    def __init__(self, uimanager):
        self._uimanager = uimanager
        
    def OnStretch(self, axis):
        pass
    
    def OnShrink(self, axis):
        pass
    
    def OnStop(self, axis):
        pass
    
    def OnTapEnter(self, axis):
        pass
    
    def OnTapLeave(self, axis):
        pass
    
    def OnTimeout(self, axis):
        self._uimanager.OnSensorCylinderTimeout()
    
class ClampCylinderListener(CT.CylinderListener):
    def __init__(self, uimanager):
        self._uimanager = uimanager
        
    def OnStretch(self, axis):
        pass
    
    def OnShrink(self, axis):
        pass
    
    def OnStop(self, axis):
        pass
    
    def OnTapEnter(self, axis):
        pass
    
    def OnTapLeave(self, axis):
        pass
    
    def OnTimeout(self, axis):
        self._uimanager.OnClampCylinderTimeout()

class ViewController():
    def __init__(self, domainobj, condomain, mainview = None):
        self.domainobj = domainobj
        self.condomain = condomain
        self.mainview = mainview
        self.resultview = None
        self.messagebox = None
        
        self.sensorcylinderlistener = SensorCylinderListener(self)
        self.clampcylinderlistener = ClampCylinderListener(self)
        
        cylinder = self.condomain.GetSensorCylinder()
        cylinder.AddListener(self.sensorcylinderlistener)
        cylinder = self.condomain.GetClampCylinder()
        cylinder.AddListener(self.clampcylinderlistener)
        
    def SetMainView(self, bookview):
        self.mainview = bookview
        self.UpdateDistance()
        self.UpdateZeroPV()
        self.UpdateTolerData()
        
    def GetMainView(self):
        return self.mainview
        
    def DetachMainView(self):
        self.mainview = None
        
    def SetResultView(self, view):
        self.resultview = view
        self.UpdateTolerData()
        
    def GetResultView(self):
        return self.resultview
    
    def DetachResultView(self):
        self.resultview = None
        
    def OnSavePref(self, pref):
        self.UpdateDistance()
        self.UpdateZeroPV()
        
    def UpdateFocResult(self, t, a, ok):
        if self.resultview:
            wx.CallAfter(self.resultview.SetFocResult, t, a, ok)
            
    def UpdateBarcode(self, barcode, ok):
        if self.resultview:
            wx.CallAfter(self.resultview.SetBarcode, barcode, ok)
        
    def UpdateDistance(self):
        pass
            
    def UpdateZeroPV(self):
        if self.mainview:
            pvs = self.domainobj.GetLeftZeroPV()
            wx.CallAfter(self.mainview.SetLeftZeroPV, pvs[0], pvs[1], pvs[2])
            pvs = self.domainobj.GetRightZeroPV()
            wx.CallAfter(self.mainview.SetRightZeroPV, pvs[0], pvs[1], pvs[2])
            
    def UpdatePartPV(self, pv1, pv2, pv3, pv4, pv5, pv6):
        if self.mainview:
            wx.CallAfter(self.mainview.SetLeftPartPV, pv1, pv2, pv3)
            wx.CallAfter(self.mainview.SetRightPartPV, pv4, pv5, pv6)
        
    def UpdateSensorData(self, pv1, pv2, pv3, pv4, pv5, pv6):
        #print pv1,pv2,pv3,pv4,pv5,pv6
        if self.mainview:
            wx.CallAfter(self.mainview.SetLeftRealPV, pv1, pv2, pv3)
            wx.CallAfter(self.mainview.SetRightRealPV, pv4, pv5, pv6)
            
    def UpdateLeftResult(self, consangle, consresult, cambangle, cambresult):
        if self.mainview:
            wx.CallAfter(self.mainview.SetLeftResult, consangle, consresult, cambangle, cambresult)
        if self.resultview:
            wx.CallAfter(self.resultview.SetLeftResult, consangle, consresult, cambangle, cambresult)
    
    def UpdateRightResult(self, consangle, consresult, cambangle, cambresult):
        if self.mainview:
            wx.CallAfter(self.mainview.SetRightResult, consangle, consresult, cambangle, cambresult)
        if self.resultview:
            wx.CallAfter(self.resultview.SetRightResult, consangle, consresult, cambangle, cambresult) 
            
    def UpdateLenResult(self, len, len_result, lendiff, len_diff_result):
        if self.mainview:
            wx.CallAfter(self.mainview.SetLenResult, len, len_result, lendiff, len_diff_result)
        if self.resultview:
            wx.CallAfter(self.resultview.SetLenResult, len, len_result, lendiff, len_diff_result)
            
    def UpdateResult(self, result):
        if result:
            wx.CallAfter(Globals.mainframe.SetRunMsg, u'测试合格', wx.Colour(0, 255, 0))
        else:
            wx.CallAfter(Globals.mainframe.SetRunMsg, u'测试不合格', wx.Colour(255, 0, 0))
        info = self.domainobj.GetCountInfo()
        wx.CallAfter(Globals.mainframe.OnUpdateCount, *info)
            
    def UpdateTolerData(self):
        dat = self.domainobj.GetTolerData()
        if self.mainview:
            wx.CallAfter(self.mainview.SetLeftConsTolerInfo, dat[0], dat[1])
            wx.CallAfter(self.mainview.SetLeftCambTolerInfo, dat[2], dat[3])
            wx.CallAfter(self.mainview.SetRightConsTolerInfo, dat[4], dat[5])
            wx.CallAfter(self.mainview.SetRightCambTolerInfo, dat[6], dat[7])
        if self.resultview:
            wx.CallAfter(self.resultview.SetLeftConsTolerInfo, dat[0], dat[1])
            wx.CallAfter(self.resultview.SetLeftCambTolerInfo, dat[2], dat[3])
            wx.CallAfter(self.resultview.SetRightConsTolerInfo, dat[4], dat[5])
            wx.CallAfter(self.resultview.SetRightCambTolerInfo, dat[6], dat[7])
            
    def OnPartReady(self):
        wx.CallAfter(Globals.mainframe.SetRunMsg, u'开始测试......', wx.Colour(0, 0, 255))
        
    def OnSensorCylinderTimeout(self):
        self.ShowBusyMessage(u'传感器气缸运动超时，请检查气缸感应器是否在正确的位置并确保气缸没有被卡住')
    
    def OnClampCylinderTimeout(self):
        self.ShowBusyMessage(u'夹紧气缸运动超时，请检查气缸感应器是否在正确的位置并确保气缸没有被卡住')
        
    def OnPartTypeChanged(self, typename):
        wx.CallAfter(Globals.mainframe.SetPartType, typename)
    
    def ShowMessage(self, msg):
        wx.MessageBox(msg)
        
    def ShowBusyMessage(self, msg, title=u'提示', timeout=1, warn = True):
        def _ShowBusyMsg(msg, warn):
            if warn:
                app.condomain.Red()
            busy = PBI.PyBusyInfo(msg, parent=None, title=title,
                              icon=images.Smiles.GetBitmap())
            
            wx.Sleep(timeout)
    
            del busy
            
            if warn:
                app.condomain.Green()
        wx.CallAfter(_ShowBusyMsg, msg, warn)
        
    def ShowMessageBox(self, msg, title=u'提示'):
        def _ShowBox(msg):
            busy = PBI.PyBusyInfo(msg, parent=None, title=title,
                              icon=images.Smiles.GetBitmap())
            self.messagebox = busy
        wx.CallAfter(_ShowBox, msg)
        
    def HideMessageBox(self):
        def _HideBox():
            del self.messagebox
            self.messagebox = None
        if self.messagebox != None:
            wx.CallAfter(_HideBox)
        
        